//
//  DemoChangBackGroundViewController.m
//  DemoChangBackGround
//
//  Created by duc nguyen van on 8/7/11.
//  Copyright 2011 STU. All rights reserved.
//

#import "DemoChangBackGroundViewController.h"

@implementation DemoChangBackGroundViewController

@synthesize viewOne;
@synthesize viewTwo;
@synthesize viewThree;
@synthesize viewFour;
//@synthesize viewProperty;

@synthesize labelOne;
@synthesize labelTwo;
@synthesize labelThree;
@synthesize labelFour;

#pragma mark processView

/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
#pragma mark processView
-(IBAction)AddNewView:(id)sender{
	
	PrimaryView *pView = [[[PrimaryView alloc] init] autorelease];

	[pView setFrame:CGRectMake(rand() % (int)self.view.frame.size.width,
							   rand() % (int)self.view.frame.size.height, 100,100)];
	pView.backgroundColor = [UIColor redColor];
	pView.delegate = self;
	[_viewArray addObject:pView];
	[self.view addSubview:pView];
}

-(void)removeSelecteView{
	viewSelected.layer.borderColor = [[UIColor whiteColor] CGColor];
	viewSelected.layer.borderWidth = 0;
	//viewSelected.layer.shadowRadius = 3;
	//viewSelected.layer.shadowOpacity = 0.5;
}

- (void)setSelectedView{
	viewSelected.layer.borderColor = [[UIColor whiteColor] CGColor];
	viewSelected.layer.borderWidth = 5;
	//viewSelected.layer.shadowRadius = 3;
	//viewSelected.layer.shadowOpacity = 0.5;
}

#pragma mark deltegatePrimaryView

-(void)touchesViewBegan:(NSSet *)touches withEvent:(UIEvent *)event andView:(UIView *)v{
	[self removeSelecteView];
	viewSelected = v;
	editController.view.hidden = NO;
	[self setSelectedView];
}

-(void)touchesViewMoved:(NSSet *)touches withEvent:(UIEvent *)event andView:(UIView*)v{
	
	UITouch *touch = [touches anyObject];
	
	CGPoint pMoved =[touch locationInView:self.view];
	v.center = pMoved;
}

#pragma mark delegateEditorViewControler

- (void)checkButton:(id)sender{
	int tag = [sender tag];
	if(tag == 1)viewSelected.backgroundColor = [UIColor blueColor];
	if(tag == 2)viewSelected.backgroundColor = [UIColor yellowColor];
	if(tag == 3)viewSelected.backgroundColor = [UIColor redColor];
	if(tag == 4)viewSelected.backgroundColor = [UIColor greenColor];
	if(tag == 5)editController.view.hidden = YES;
}

#pragma mark tap event

- (void)handleTap:(UITapGestureRecognizer*)tap
{	
	NSLog(@"View tapped");
	//viewProperty.hidden = YES;
	if (editController.view.hidden == YES) {
		[self removeSelecteView];
	}
}

- (void)handelTapSubView:(UITapGestureRecognizer*)tap{
	NSLog(@"SubView tapped");
}

- (void)handleTapOne:(UITapGestureRecognizer*)tap
{	
	//NSLog(@"ViewOne taped");
	editController.view.hidden = NO;
	//textField.text = labelOne.text;
	
	[self removeSelecteView];
	viewSelected = viewOne;
	[self setSelectedView];
}
- (void)handleTapTwo:(UITapGestureRecognizer*)tap
{	
	//NSLog(@"ViewTwo taped");
	editController.view.hidden = NO;
	//textField.text = labelTwo.text;
	
	[self removeSelecteView];
	viewSelected = viewTwo;
	[self setSelectedView];
}

- (void)handleTapThree:(UITapGestureRecognizer*)tap
{	
	//NSLog(@"ViewThree taped");
	editController.view.hidden = NO;
	//textField.text = labelThree.text;
	
	[self removeSelecteView];
	viewSelected = viewThree;
	[self setSelectedView];
}

- (void)handleTapFour:(UITapGestureRecognizer*)tap
{	
	//NSLog(@"ViewFour taped");
	editController.view.hidden = NO;
	//textField.text = labelFour.text;
	
	[self removeSelecteView];
	viewSelected = viewFour;
	[self setSelectedView];
}

- (void)handleTapEdit:(UITapGestureRecognizer*)tap
{	
	NSLog(@"ViewPro taped");
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
	NSLog(@"%@", [[touch.view class] description]);
	if ([[[touch.view class] description] isEqualToString:@"UIRoundedRectButton"]) {
		return NO;
	}
	if ([[[touch.view class] description] isEqualToString:@"UIButton"]) {
		return NO;
	}
	else {
		return YES;
	}
	
}

#pragma mark init

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	//set deletegate
	viewOne.delegate = self;
	viewTwo.delegate = self;
	viewThree.delegate = self;
	viewFour.delegate = self;
	
	if(editController == nil) editController = [[EditorViewControler alloc] init];
	[editController.view setFrame:CGRectMake(400, 400, editController.view.bounds.size.width, editController.view.bounds.size.height)];
	editController.delegate = self;
	editController.view.hidden = YES;
	[self.view addSubview:editController.view];
	
	
	UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
	tap.delegate = self;
	[self.view addGestureRecognizer:tap];
	[tap release];
	
	UITapGestureRecognizer* tapOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapOne:)];
	[viewOne addGestureRecognizer:tapOne];
	[tapOne release];
	
	UITapGestureRecognizer* tapTwo = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapTwo:)];
	[viewTwo addGestureRecognizer:tapTwo];
	[tapTwo release];
	
	UITapGestureRecognizer* tapThree = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapThree:)];
	[viewThree addGestureRecognizer:tapThree];
	[tapThree release];
	
	UITapGestureRecognizer* tapFour = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapFour:)];
	[viewFour addGestureRecognizer:tapFour];
	[tapFour release];
	
	UITapGestureRecognizer* tapEdit= [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapEdit:)];
	[editController.view addGestureRecognizer:tapEdit];
	tapEdit.delegate = self;
	[tapEdit release];
}


// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

@end
